package com.helloandroid.uitween.single

import android.view.View
import com.helloandroid.uitween.UITween

open class ScaleTo(val duration: Float, var dstSx: Float, var dstSy: Float) : UITween() {
    var firstExecute = false

    var psx = 0f
    var psy = 0f
    var csx = 0f
    var csy = 0f
    var elapsedTime = 0f

    override fun initialize(node: View) {
        csx = node.scaleX
        csy = node.scaleY
        psx = (dstSx - csx) / duration
        psy = (dstSy - csy) / duration
        elapsedTime = 0f
    }

    override fun execute(dt: Float) {
        val node = target!!
        if (!firstExecute) {
            initialize(node)
            firstExecute = true
        }

        elapsedTime += dt
        if (elapsedTime >= duration) {
            node.scaleX = dstSx
            node.scaleY = dstSy
            end = true
        } else {
            csx += psx * dt
            csy += psy * dt
            node.scaleX = csx
            node.scaleY = csy
        }
    }

    override fun clone(): UITween {
        return ScaleTo(duration,dstSx,dstSy)
    }
}

class ScaleBy( duration: Float,val dxSx:Float,val dxSy:Float): ScaleTo(duration,0f,0f){
    override fun initialize(node: View) {
        csx = node.scaleX
        csy = node.scaleY
        dstSx = csx * dxSx
        dstSy = csy * dxSy
        psx = dxSx / duration
        psy = dxSy / duration
        elapsedTime = 0f
    }

    override fun clone(): UITween {
        return ScaleBy(duration,dxSx,dxSy)
    }
}
